import Vue from 'vue'
import Vuex from 'vuex'
import * as auth from '@/utils/auth'
import { loginApi } from '@/api/user'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: auth.getToken() || {}
  },
  mutations: {
    setToken (state, token) {
      state.user = token
      auth.setToken(token)
    },
    delToken (state) {
      state.user = {}
      auth.delToken()
    }
  },
  actions: {
    async getToken (context, form) {
      const { data } = await loginApi(form)
      context.commit('setToken', data)
    }
  },
  modules: {
  }
})
